package com.snail.ddclock

import android.app.Application
import android.content.Context
import android.os.Handler
import android.util.Log
import com.snail.commons.AppHolder
import com.umeng.commonsdk.UMConfigure
import com.umeng.message.IUmengRegisterCallback
import com.umeng.message.PushAgent
import com.umeng.message.UmengMessageHandler
import com.umeng.message.entity.UMessage
import org.json.JSONObject
import java.util.*
import javax.mail.Address
import javax.mail.Session
import javax.mail.internet.InternetAddress
import javax.mail.internet.MimeBodyPart
import javax.mail.internet.MimeMessage
import javax.mail.internet.MimeMultipart


/**
 *
 *
 * date: 2019/7/23 09:49
 * author: zengfansheng
 */
class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        AppHolder.init(this)
        UMConfigure.init(this, "589d7203b27b0a62c0000cdc", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "75518dae0c34aea7f1fae094b83e243b")
        //获取消息推送代理示例
        val mPushAgent = PushAgent.getInstance(this)
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(object : IUmengRegisterCallback {
            override fun onSuccess(deviceToken: String) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i(TAG, "注册成功：deviceToken：-------->  $deviceToken")
            }

            override fun onFailure(s: String, s1: String) {
                Log.e(TAG, "注册失败：-------->  s:$s,s1:$s1")
            }
        })
        val handler = Handler()
        val messageHandler = object : UmengMessageHandler() {
            override fun dealWithCustomMessage(context: Context, msg: UMessage) {
                Log.i(TAG, "自定义消息：-------->  $msg")
                handler.post {
                    try {
                        val json = JSONObject(msg.custom)
                        if (json["action"] == "com.alibaba.android.rimet") {
                            val i = packageManager.getLaunchIntentForPackage("com.alibaba.android.rimet")
                            if (i != null) {
                                context.startActivity(i)                                
                                sendTextMail()
                            }
                        }
                    } catch (e: Exception) {
                        Log.e(TAG, e.message, e)
                    }
                }
            }
        }
        mPushAgent.messageHandler = messageHandler
    }
    companion object {
        private const val TAG = "MyApplication"

        fun sendTextMail() {
            Thread {
                val mailInfo = MailInfo()
                mailInfo.userName = "bingmo977"
                mailInfo.password = "b000000"
                mailInfo.fromAddress = "bingmo977@163.com"
                mailInfo.toAddress = "bingmo977@163.com"
                mailInfo.mailServerHost = "smtp.163.com"
                mailInfo.subject = "钉钉打卡"
                mailInfo.content = "执行调起钉钉极速打卡"
                // 根据邮件会话属性和密码验证器构造一个发送邮件的session
                val session = Session.getInstance(mailInfo.getProperties())
                try {
                    // 根据session创建一个邮件消息
                    val message = MimeMessage(session)
                    // 设置邮件消息的发送者
                    message.setFrom(InternetAddress(mailInfo.fromAddress))
                    // 设置邮件消息的主题
                    message.subject = mailInfo.subject
                    // 设置邮件消息发送的时间
                    message.sentDate = Date()
                    // 创建邮件正文，为了避免邮件正文中文乱码问题，需要使用CharSet=UTF-8指明字符编码
                    val text = MimeBodyPart()
                    text.setContent(mailInfo.content, "text/html;charset=UTF-8")
                    // 创建容器描述数据关系
                    val mp = MimeMultipart()
                    mp.addBodyPart(text)
                    message.setContent(mp)
                    val transport = session.transport
                    // 连接邮件服务器  
                    transport.connect(mailInfo.userName, mailInfo.password)
                    // 发送邮件  
                    transport.sendMessage(message, arrayOf<Address>(InternetAddress(mailInfo.fromAddress)))
                    // 关闭连接  
                    transport.close()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }.start()
        }
    }
}